package com.igirl.manage.controller;

import com.github.abel533.entity.Example;
import com.igirl.manage.pojo.ContentCategory;
import com.igirl.manage.service.content.ContentCategoryService;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.protocol.HTTP;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * @Author: jiaheng
 * @Description:
 * @Date: Created in 2017/12/31
 * @Modified:
 */
@Controller
@RequestMapping("content/category")
public class ContentCategoryController {

    private Logger logger = LoggerFactory.getLogger(ContentCategoryController.class);

    @SuppressWarnings("SpringJavaAutowiringInspection")
    @Autowired
    private ContentCategoryService contentCategoryService;

    @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity<List<ContentCategory>> queryAllContentCategory(@RequestParam(value = "id",defaultValue = "0") Long id) {
        List<ContentCategory> categoryList = null;
        try {
            ContentCategory category = new ContentCategory();
            category.setParentId(id);
            categoryList = this.contentCategoryService.queryListByWhere(category);
            if (categoryList == null) {
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
            }
            return ResponseEntity.ok(categoryList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<ContentCategory> saveCategory(ContentCategory contentCategory) {
        try {
            if(contentCategory == null || StringUtils.isEmpty(contentCategory.getName())) {
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
            }
            ContentCategory category = this.contentCategoryService.saveCategoryAndUpdateParentCategory(contentCategory);
            return ResponseEntity.ok(category);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

    @RequestMapping(method = RequestMethod.DELETE)
    public ResponseEntity<Void> deleteCategory(ContentCategory contentCategory) {

        try {
            if(contentCategory == null || StringUtils.isEmpty(contentCategory.getId().toString())) {
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
            }
            this.contentCategoryService.deleteCategory(contentCategory);
            return ResponseEntity.ok(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @RequestMapping(method = RequestMethod.PUT)
    public ResponseEntity<Void> updateCategoryName(ContentCategory contentCategory) {
        try {
            if(contentCategory == null || StringUtils.isEmpty(contentCategory.getName()) || StringUtils.isEmpty(contentCategory.getId().toString())) {
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
            }
            this.contentCategoryService.updateSelective(contentCategory);
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

}
